package com.egoo.ticket.server.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.egoo.ticket.server.config.redis.RedisKeyConstant;
import com.egoo.ticket.server.pojo.dto.NonSysDictParamDto;
import com.egoo.ticket.server.pojo.dto.SysDictChildrenDto;
import com.egoo.ticket.server.pojo.dto.SysDictDto;
import com.egoo.ticket.server.service.SysDictService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.RedisUtil;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author：victor_tang
 * @Date ：2020/1/6
 * @Description：系统字典管理控制器
 */
@Slf4j
@RestController
@RequestMapping(value = "/v1/sysDict", produces = "application" +
		"/json")
@Api(value = "系统数据字典管理",tags = "系统数据字典管理",description = "系统数据字典管理")
public class SysDictController {

	@Autowired
	private SysDictService sysDictService;
	@Autowired
	private RedisUtil redisUtil;

	@ApiOperation(value = "根据字典编码获取字典详情列表", notes = "根据字典编码获取字典详情列表")
	@RequestMapping(method = RequestMethod.GET, value ="/getDictDetailListByPcode")
	public DbResponse getDictDetailListByDictcode(@ApiParam(value = "dictCode", required = true)
									@RequestParam(name = "dictCode") String dictCode,
												  HttpServletRequest request) {
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		if (StringUtils.isEmpty(dictCode)) {
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "字典编码不能为空！");
		}
		try {
			// 查询字典详情列表
			return sysDictService.getDictDetailListByDictcode(dictCode,tenantId);

		} catch (Exception e) {
			log.error("字典详情列表查询异常：" + e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "字典详情列表查询异常：" + e);
		}
	}

	@ApiOperation(value = "修改对应字典",notes = "修改对应字典")
	@PostMapping(value = "/updateSysDict")
	public DbResponse updateSysDict(@RequestBody SysDictDto sysDictDto){
		if (ObjectUtils.isEmpty(sysDictDto)){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"参数不能为空！");
		}
		if (StringUtils.isEmpty(sysDictDto.getId())){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"字典id不能为空！");
		}
		return sysDictService.updateSysDict(sysDictDto);
	}

	@ApiOperation(value = "获取参数设置的字典列表",notes = "获取参数设置的字典列表")
	@GetMapping(value = "/selectParamSetList")
	public DbResponse selectParamSetList(HttpServletRequest request){
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		return sysDictService.getParamSetList(tenantId);
	}

	@ApiOperation(value = "修改非数据字典类参数设置",notes = "修改非数据字典类参数设置")
	@PostMapping(value = "/updateNoSysDictParams")
	public DbResponse updateNoSysDictParams(@RequestBody NonSysDictParamDto nonSysDictParam){
		if (ObjectUtils.isEmpty(nonSysDictParam)){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"参数不能为空！");
		}
		if (StringUtils.isEmpty(nonSysDictParam.getCode())){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"code不能为空！");
		}
		if (RedisKeyConstant.WORK_ORDER_URGE.equals(nonSysDictParam.getCode())){
			// 设置催单时间间隔
			redisUtil.set(RedisKeyConstant.WORK_ORDER_URGE, nonSysDictParam.getValue());
		}
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,"修改成功！");
	}

	@ApiOperation(value = "新增字典数据",notes = "新增字典数据")
	@PostMapping(value = "/add")
	public DbResponse add(@RequestBody JSONObject jsonObject,
						  HttpServletRequest request){
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		JSONArray jsonArray = jsonObject.getJSONArray("sysDictChildrenDtoList");


		if (CollectionUtils.isEmpty(jsonArray)){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"新增参数不能为空");
		}
		List<SysDictChildrenDto> sysDictChildrenDtos = JSON.parseArray(jsonArray.toJSONString(), SysDictChildrenDto.class);
		SysDictDto sysDictDto = new SysDictDto();
		sysDictDto.setSysDictChildrenDtoList(sysDictChildrenDtos);
		sysDictDto.setTenantId(tenantId);
		DbResponse dbResponse = sysDictService.add(sysDictDto);
		return dbResponse;
	}

	@ApiOperation(value = "删除字典数据",notes = "删除字典数据")
	@PostMapping(value = "/delete")
	public DbResponse delete(@RequestBody SysDictDto sysDictDto,
						  HttpServletRequest request){
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);

		List<String> ids = sysDictDto.getIds();
		if (CollectionUtils.isEmpty(ids)){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"请选中要删除的数据");
		}
		sysDictDto.setTenantId(tenantId);
		DbResponse dbResponse = sysDictService.delete(sysDictDto);
		return dbResponse;
	}

}
